package com.udp;

import java.io.IOException;
import java.net.*;

public class Send01 {
    public static void main(String[] args) throws IOException {
        DatagramSocket socket=new DatagramSocket(9999);
        //将要发送的数据打包
        byte[]bytes="good afternoon".getBytes();
        DatagramPacket datagramPacket = new DatagramPacket(bytes, bytes.length, InetAddress.getLocalHost(), 8888);//(byte[] length 主机 端口名)
        //
        socket.send(datagramPacket);
        System.out.println("数据已经发送");
        //接受回复的消息
        byte []getmse=new byte[1024];
        DatagramPacket packet=new DatagramPacket(getmse,getmse.length);
        socket.receive(packet);
          //拆包 取数据 显示
        int len=packet.getLength();
        byte []ms=packet.getData();
        System.out.println("收到消息"+new String(ms,0,len));
        //关闭
        socket.close();
    }
}
